<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      background-color: rgb(204, 145, 36);
    }
  </style>
</head>
<body>
  <div id="app">
    <h2>开始学习路由</h2>
    <router-link :to="{name:'首页'}">到首页</router-link>
    <button @click="$router.push({name:'新闻'})">到新闻页</button>
    <button @click="$router.push('/about')">到关于我们</button>
    
    <hr>
    <!-- 视图上定义一个 出口 路由组件渲染的地方 -->
    <router-view></router-view>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    // 定义好三个组件 参数对象
    const Home = {
      template: `
        <div>
          <h1>我是首页</h1>  
        </div>
      `,
      mounted(){
        console.log(this.$router, 222);
      }
    }
    const  News = {
      template: `
        <div>
          <h1>我是新闻页</h1>  
        </div>
      `
    }
    const About = {
      template: `
        <div>
          <h1>我是关于我们页</h1>  
        </div>
      `
    }

    const NotFound = {
      template: `
        <div>
          <h1>我是404页</h1>  
        </div>
      `
    }

    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        name: '首页',
        component: Home
      },
      {
        path: '/news',
        name: '新闻',
        component: News
      },
      {
        path: '/about',
        name: '关于我们',
        component: About
      },
      {
        path: '*',
        component: NotFound
      }
    ]
    const Join = {
      template: `
        <div>
          <h1>加入我们</h1>  
        </div>
      `
    }
    // 创建路由对象实例
    const router = new VueRouter({
      // routes 路由规则
      routes
    })
    // console.log(router, 111);
    const vm = new Vue({
      el: '#app',
      router,
      methods: {
        
      }
    })
  </script>
</body>
</html>